package com.lm.xstream.example;
import com.lm.xstream.converter.PersonConverter;
import com.lm.xstream.ele.Person;
import com.lm.xstream.ele.Person2;
import com.thoughtworks.xstream.XStream;

public class ConverterExample {
    public static void main(String[] args) {
        XStream xstream = new XStream();
        xstream.registerConverter(new PersonConverter());

        // 安全设置：允许反序列化的类
        XStream.setupDefaultSecurity(xstream);
        xstream.allowTypes(new Class[] {Person.class});

        // 或者使用通配符，允许指定包下的类
        xstream.allowTypesByWildcard(new String[] {"com.lm.xstream.ele.**"});

        // 创建对象
        Person2 person = new Person2("John", 30);

        // 对象转 XML
        String xml = xstream.toXML(person);
        System.out.println(xml);

        // XML 转对象
        Person2 newPerson = (Person2) xstream.fromXML(xml);
        System.out.println("Name: " + newPerson.getName());
        System.out.println("Age: " + newPerson.getAge());
    }
}
